var source = require('./source/historySource.js');
var async = require('async');
var request = require('request');
var schedule = require('node-schedule');
//var readability = require('node-readability-cheerio');
var cheerio = require('cheerio');
//var idxSymbolMap = require('./QuotesUtilities.js').idxSymbolMap;
var mysqlDao = require('./dao/mysqlDao.js');
function date2strlocal(dateObj, format) {
    var yyyy = dateObj.getFullYear();
    var mm = dateObj.getMonth() + 1;
    var dd = dateObj.getDate();
    if (mm < 10) {
        mm = '0' + mm;
    }
    if (dd < 10) {
        dd = '0' + dd;
    }

    switch (format) {
        case 'yyyymmdd':
            return yyyy + '-' + mm + '-' + dd;
            break;
        case 'mmddyyyy':
            return mm + '-' + dd + '-' + yyyy;
            break;
        default :
            return yyyy + '-' + mm + '-' + dd;
    }
}
function extractForm($, headSelector, rowSelector, eleSelector) {
    var headArray = [];
    if (headSelector instanceof Array) {
        headArray = headSelector;
    }
    else if (typeof headSelector === 'string') {
        var head = $(headSelector).children(eleSelector);
        for (var j = 0; j < head.length; j++) {
            headArray[j] = $(head[j]).text();
        }
    }
    else {
        throw new Error('Error type of headSelector');
    }

    var rows = $(rowSelector);
    var colNum = headArray.length;
    var rowNum = rows.length;
    var result = [];
    for (var j = 0; j < rowNum; j++) {
        var obj = {};
        for (var k = 0; k < colNum; k++) {
            var elements = $(rows[j]).children(eleSelector);
            obj[headArray[k]] = $(elements[k]).text().trim();
        }
        result.push(obj);
    }
    return result;
}
function date2strutc(dateObj, format) {
    var yyyy = dateObj.getUTCFullYear();
    var mm = dateObj.getUTCMonth() + 1;
    var dd = dateObj.getUTCDate();
    if (mm < 10) {
        mm = '0' + mm;
    }
    if (dd < 10) {
        dd = '0' + dd;
    }

    switch (format) {
        case 'yyyymmdd':
            return yyyy + '-' + mm + '-' + dd;
            break;
        case 'mmddyyyy':
            return mm + '-' + dd + '-' + yyyy;
            break;
        default :
            return yyyy + '-' + mm + '-' + dd;
    }
}
function getHisQuotes_Investing(options) {
    // async.mapLimit(['Weekly'], 1, function (type, totalCb) {
    async.mapLimit(['Daily', 'Weekly', 'Monthly'], 1, function (type, totalCb) {
        console.log(type);
        async.mapLimit(options, 4, function (item, cb) {
            var form = {
                action: 'historical_data',
                curr_id: item.investingID,
                st_date: item.start,
                end_date: item.end,
                interval_sec: type
            };
            var reqOptions = {
                url: 'http://www.investing.com/instruments/HistoricalDataAjax',
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
                    'X-Requested-With': 'XMLHttpRequest'
                },
                timeout: 20000,
                form: form
            };
            request.post(reqOptions, function (err, res, body) {
                if (err) {
                    console.log('#####' + reqOptions.form.curr_id);
                    cb(err);
                    return;
                }
                console.log('#####' + reqOptions.form.curr_id);
                var $ = cheerio.load(body);
                if ($('#curr_table > tbody tr').text().indexOf('No results found') != -1) {
                    return callback(null, []);
                }
                var result = extractForm($,
                    ['date', 'close', 'open', 'high', 'low', 'volume', 'changeinpercent'],
                    '#curr_table > tbody tr', 'td');
                for (var j = 0; j < result.length; j++) {
                    result[j].symbol = item.etf_symbol;
                    result[j].name = item.name;
                    result[j].date = date2strlocal(new Date(result[j].date));
                    result[j].nameen = '';
                    result[j].namecn = '';
                    result[j].country = '';
                    if (result[j].volume == '-') {
                        result[j].volume = '';
                    }
                }
                cb(null, result);
            });
        }, function (err, data) {
            if (err) {
                totalCb(err);
                return;
            }
            totalCb(null, data);
        });
    }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        }
        //console.log(data);
        mysqlDao.toHistory(data);
    });
}

getHisQuotes_Investing(source);